import { defineStore } from 'pinia'
import { asnycRoute } from '@/router/routes'
import type {
  ImenuList,
  IsettingsState,
  Isetting,
  Itheme,
} from './types/settings'
type IsettingKeys = keyof Omit<Isetting, 'theme'>
type IthemeKeys = keyof Itheme

export const useSettingsStore = defineStore('settings', {
  state: (): IsettingsState => {
    return {
      menuList: asnycRoute as ImenuList[],
      fold: false, // 是否折叠
      refsh: false, // 是否刷新
      settingsData: {
        // 面包屑
        breadcrumb: true,
        // 面包屑图标
        breadcrumbIcon: true,
        // 折叠图标
        hamburger: true,
        // 全屏图标
        screenfull: true,
        // 尺寸图标
        size: true,
        // 多语言图标
        locale: true,
        // 标签页
        tagsView: true,
        // 标签页图标
        getTagsViewIcon: true,
        // logo
        logo: true,
        // 菜单手风琴
        uniqueOpened: false,
        // 固定header
        fixedHeader: true,
        // 页脚
        footer: true,
        // 灰色模式
        greyMode: false,
        // 暗黑模式
        isDark: false,
        // 开启水印
        watermark: false,
        // 主题相关
        theme: {
          // 左侧菜单栏主题
          sideBarActive: 0,
          // 顶部菜单栏主题
          topBarActive: 2,
          // 主题颜色
          colorActive: 3,
          // 布局模式
          layoutActive: 0,
        },
      },
    }
  },
  actions: {
    // 获取到浏览器存储得数据并且赋值
    GET_LOCALSTORAGE() {
      const settingsData = localStorage.getItem('settingsData')
      if (settingsData) {
        this.settingsData = JSON.parse(settingsData)
      } else {
        localStorage.setItem('settingsData', JSON.stringify(this.settingsData))
      }
    },
    SET_SETTINGS_DATA(key: IsettingKeys | IthemeKeys, value: number | boolean) {
      if ((key as IthemeKeys) in this.settingsData.theme) {
        this.settingsData.theme[key as IthemeKeys] = value as number
      } else {
        this.settingsData[key as IsettingKeys] = value as boolean
      }
      console.log(this.settingsData, 'this.settingsData')
      localStorage.setItem('settingsData', JSON.stringify(this.settingsData))
    },
  },
})
